<?php
class CImage {
	
	public $oim;
	
	public function __construct() {
	
	}
	
	public function cutImage($image, $maxy, $type) {
		try {
			
			switch (strtolower ( $type )) {
				case 'jpg' :
					$im = @imagecreatefromjpeg ( $image );
					
					break;
				
				case 'jpeg' :
					$im = @imagecreatefromjpeg ( $image );
					break;
				
				case 'gif' :
					$im = @imagecreatefromgif ( $image );
					break;
				
				case 'png' :
					$im = @imagecreatefrompng ( $image );
					break;
				
				case 'bmp' :
					$im = @imagecreatefromwbmp ( $image );
					break;
			}
			
			$curx = imagesx ( $im );
			$cury = imagesy ( $im );		
			if($maxy<$cury){
				$ratio = $maxy / $cury;
				$newx = $curx * $ratio;
				$newy = $cury * $ratio;			
			}else{			
				$newx=$curx;				
				$newy=$cury;		
			}
			
			$this->oim = imagecreatetruecolor ( $newx, $newy );
			
			imagecopyresized ( $this->oim, $im, 0, 0, 0, 0, $newx, $newy, $curx, $cury );
		
		} catch ( Exception $e ) {
			
			echo $e->getMessage ();
		
		}
	
	}
	
	public function showImage($type) {
		switch (strtolower ( $type )) {
			case 'jpg' :
				header ( "content-type: image/jpeg" );
				imagejpeg ( $this->oim );
				break;
			
			case 'jpeg' :
				header ( "content-type: image/jpeg" );
				imagejpeg ( $this->oim );
				break;
			
			case 'gif' :
				header ( "content-type: image/gif" );
				imagegif ( $this->oim );
				break;
			
			case 'png' :
				header ( "content-type: image/png" );
				imagepng ( $this->oim );
				break;
			
			case 'bmp' :
				header ( "content-type: image/bmp" );
				imagewbmp ( $this->oim );
				break;
		}
	}
	
	public function yanzheng($sessionID) {
		$type = 'gif';
		$width = 60;
		$height = 25;
		header ( "Content-type:image/" . $type );
		srand ( ( double ) microtime () * 1000000 );
		
		$randval = CString::randYzStr ( 4, "" );
		
		if ($type != 'gif' && function_exists ( 'imagecreatetruecolor' )) {
			$im = @imagecreatetruecolor ( $width, $height );
		} else {
			$im = @imagecreate ( $width, $height );
		}
		
		$r = array (225, 211, 255, 223 );
		$g = array (225, 236, 166, 125 );
		$b = array (225, 236, 166, 125 );
		
		$key = rand ( 0, 3 );
		$backColor = ImageColorAllocate ( $im, $r [$key], $g [$key], $b [$key] );
		$borderColor = ImageColorAllocate ( $im, 0, 0, 0 );
		$pointColor = ImageColorAllocate ( $im, 255, 170, 255 );
		
		@imagefilledrectangle ( $im, 0, 0, $width - 1, $height - 1, $backColor );
		@imagerectangle ( $im, 0, 0, $width - 1, $height - 1, $borderColor );
		$stringColor = ImageColorAllocate ( $im, 255, 51, 153 );
		
		for($i = 0; $i <= 100; $i ++) {
			$pointX = rand ( 2, $width - 2 );
			$pointY = rand ( 2, $height - 2 );
			@imagesetpixel ( $im, $pointX, $pointY, $pointColor );
		}
		
		@imagestring ( $im, 5, 10, 5, $randval, $stringColor );
		$ImageFun = 'Image' . $type;
		$ImageFun ( $im );
		
		@ImageDestroy ( $im );
		
		$_SESSION [$sessionID] = $randval;
	
	}
	
	public function saveImage($image, $maxy, $type, $path) {
		
		$imgpath = preg_replace ( "/\/[^\/\.]+\." . $type . "$/", "", $path ); //分解出纯路径，不带文件名和后缀
		if (! @opendir ( $imgpath )) {
			Zend_Loader::loadClass("CFile");
			$isTrue = CFile::createFolder ( $imgpath );
			if (! $isTrue) {
				$this->setError ( "新建文件夹错误!" );
				return false;
			}
		}
		
		if (! @file_get_contents ( $image )) {
			$this->error = "图片地址无效！请更改地址！";
			return false;
		}
		
		$this->cutImage ( $image, $maxy, $type );
		
		switch (strtolower ( $type )) {
			case 'jpg' :
				imagejpeg ( $this->oim, $path );
				break;
			
			case 'jpeg' :
				imagejpeg ( $this->oim, $path );
				break;
			
			case 'gif' :
				imagegif ( $this->oim, $path );
				break;
			
			case 'png' :
				imagepng ( $this->oim, $path );
				break;
			
			case 'bmp' :
				imagewbmp ( $this->oim, $path );
				break;
		}
		return true;
	}
	
	public function cutPhoto($image, $maxy, $path) { //$path仅包含路径
		
		$imageArray2 = explode ( "/", $image );
		
		$imgfile = $imageArray2 [count ( $imageArray2 ) - 1];
		
		$imgFileArray = explode ( ".", $imgfile );
		
		$imgType = $imgFileArray [count ( $imgFileArray ) - 1];
		
		$imgName = time () . $imgFileArray [0];		
		
		$imgName = $imgName . '.' . $imgType;
		
		$this->saveImage( $image, $maxy, $imgType, $path . $imgName );

		return $imgName;
	}
	
	
	//产生随机字符串
	public function randStr($len=5,$format='CHAR') { 
		switch($format) { 
			case 'ALL':
			$chars='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; break;
			case 'CHAR':
			$chars='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'; break;
			case 'NUMBER':
			$chars='0123456789'; break;
			case 'FIRM_CODE':
			$chars='ABCDEFGHJKMNPQRTUVWXYabcdefghjkmnpqrstuvwxyz346789'; break;
			default :
			$chars='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; 
			break;
		}
		$string="";
		while(strlen($string)<$len)
		$string.=substr($chars,(mt_rand()%strlen($chars)),1);
		return $string;
	}
	
	//画图
	public function image($code,$entropyLevel=3)
	{
		$type = 'gif'; // 格式
		$width= 180; // 宽
		$height= 50; // 高
		
		//所用字体
		$fontFileArray[0]=MY_ROOT.'/html/font/comic.ttf';
		$fontFileArray[1]=MY_ROOT.'/html/font/comic.ttf';
		$fontFileArray[2]=MY_ROOT.'/html/font/comic.ttf';
		$fontFileArray[3]=MY_ROOT.'/html/font/comic.ttf';
		$fontFileArray[4]=MY_ROOT.'/html/font/comic.ttf';
		//$fontFileArray[5]=MY_ROOT.'/html/font/comic.ttf';
		
		
		
		header("Content-type: image/".$type); 
		$im = @imagecreate($width,$height);
		
		ImageColorAllocate($im,255,255,255); //背景
		
		// 画字符
		$codeArray = str_split($code);
		$strX=12;
	
		for ($i=0;$i<5;$i++){
			$strColor= ImageColorAllocate($im,rand(0,222),rand(0,222),rand(0,222));//字体颜色
			$strY = rand(30,35); // y座标
			$angle = rand(-30,30); // 倾斜角度
			$fontSize = rand(20,25); // 字体大小
			
			$arraySize = count($fontFileArray);
			$fontFileArrayKey=rand(0,$arraySize-1);
			$fontStyle=$fontFileArray[$fontFileArrayKey];
			ImageTTFText($im, $fontSize, $angle, $strX, $strY, $strColor, $fontStyle, $codeArray[$i]);
			$strX =$strX+rand(30,35); // x座标++
		
		}
	
		// 画干扰线条
		for ($i=0;$i<$entropyLevel;$i++)
		{
			$lineColor= ImageColorAllocate($im,rand(0,255),rand(0,255),rand(0,255));//线条颜色
			imageline($im, rand (0,200), rand (0,60), rand (0,$width), rand (0,$height), $lineColor);
			//imagearc($im, rand (0,200), rand(0,60), rand (0,$width), rand(0,$height), rand(-360,360), rand(-360,360), $lineColor);
		}
	
	
	
		$ImageTypeFunction='Image'.$type;
		$ImageTypeFunction($im);
		@ImageDestroy($im);
	}
	
	
	public function yanzheng2($codeName = 'safeCode'){
		$code = $this->randStr(5,'FIRM_CODE');
		session_start();
		//session_register($codeName);
		$_SESSION[$codeName]=$code;
		$this->image($code);
		
	}
}

